<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>MyPet</title>
</head>

<body>
    <script>
        //用户类
        var User = function (username, password) {
            this.username = username
            this.password = password
        }
        //宠物类
        var Pet = function (nickname, gender, owner) {
            this.nickname = nickname
            this.gender = gender
            this.owner = owner
            var now = new Date
            now = now.setHours(now.getHours - 1)
            this.lastEatTime = now
            this.lastPlayTime = now
            this.eatState = 60
            this.playState = 70
            this.eat = () => {
                var now = new Date
                if (now - this.lastEatTime < 3000) {
                    alert("我刚刚吃过，过一会再吃吧主人...")
                    return
                }
                console.log(this.nickname + "正在吃...")
                this.eatState += 10
                this.lastEatTime = now
            }
            this.play = () => {
                var now = new Date
                if (now - this.lastPlayTime < 5000) {
                    alert("我刚刚吃过，过一会再吃吧主人...")
                    return
                }
                console.log(this.nickname + "和你玩的很开心...")
                this.playState += 5
                this.lastPlayTime = now
            }
            this.introduceSelf = () => {
                console.log(`我的主人${this.owner}你好：
                我叫${this.nickname}，
                我是个${this.gender === 1 ? "男" : "女"}孩子，
                我的饱食度为：${this.eatState},
                我的开心值为：${this.playState}`)
            }
        }
        //定义游戏类
        var game = {
            //模拟用户数据库
            userList: [],
            //模拟宠物数据库
            petList: [],
            //模拟敏感词库
            sensitive: ["习近平", "孙健伟", "Fuck", "fuck", "操", "鸡巴", "逼"],
            //获取当前登录名
            userLogin: null,
            //注册操作
            doRegister(username, password) {
                this.userList.push(new User(username, password))
            },
            //登录检查
            checkLogin(username, password) {
                return this.userList.some(function (item) {
                    return item.username === username && item.password === password
                })
            },
            //购买宠物操作
            doBuyPet(nickname, gender, owner) {
                this.petList.push(new Pet(nickname, gender, owner))
            },
            //检查用户名是否重复
            checkUsernameRepeat(username) {
                return this.userList.some(function (item) {
                    return item.username === username
                })
            },
            //检查用户名和昵称是否含有敏感词
            checkNameSensitive(name) {
                return this.sensitive.includes(name)
            },
            //检查是否已拥有宠物
            checkHasPet() {
                return this.petList.find((item) => {
                    return item.owner === this.userLogin
                })
            },
            //展示欢迎界面
            showWelcomePage() {
                var result = prompt("欢迎来到MyPet游戏，请选择你的操作：1.登录，2.注册，3.退出")
                switch (result) {
                    case "1":
                        this.showLoginPage()
                        break;
                    case "2":
                        this.showRegisterPage()
                        break;
                    case "3":
                        alert("您已退出")
                        break;
                    default:
                        arguments.callee.call(this)
                        break;
                }
            },
            //展示注册界面
            showRegisterPage() {
                do {
                    var username = prompt("请输入一个用户名")
                    var isOk = !username || !username.trim()
                    if (isOk) alert("用户名不得为空")
                    var isAlive = this.checkUsernameRepeat(username)
                    if (isAlive) alert("用户名已存在")
                    var isUnallow = this.checkNameSensitive(username)
                    if (isUnallow) alert("用户名包含敏感词，请重新输入")
                } while (isOk || isAlive || isUnallow)
                do {
                    var password = prompt("请输入一个6-20位的密码")
                    var isOk = !password || !password.trim() || password.length < 6 || password.length > 20
                    if (isOk) alert("密码输入有误")
                } while (isOk)
                do {
                    var password1 = prompt("请再次输入密码")
                    var isOk = password === password1
                    if (!isOk) {
                        alert("两次输入不一致，请重新注册")
                        arguments.callee.call(this)
                    }
                } while (!isOk)
                this.doRegister(username, password)
                alert("注册成功，你是第" + this.userList.length + "名注册用户")
                this.showWelcomePage()
            },
            //展示登录界面
            showLoginPage() {
                do {
                    var username = prompt("请输入你的用户名")
                    var isOk = !username || !username.trim()
                    if (isOk) alert("用户名不得为空")
                } while (isOk)
                do {
                    var password = prompt("请输入一个6-20位的密码")
                    var isOk = !password || !password.trim() || password.length < 6 || password.length > 20
                    if (isOk) alert("密码输入有误")
                } while (isOk)
                var isOk = this.checkLogin(username, password)
                if (isOk) {
                    alert("你已成功登录")
                    this.userLogin = username
                    this.showMainPage()
                } else {
                    alert("用户名或密码不正确请重新输入")
                    arguments.callee.call(this)
                }
            },
            //展示主界面
            showMainPage() {
                var result = prompt(this.userLogin + "欢迎回来，请选择你的操作：1.购买宠物，2.我的宠物，3.注销")
                switch (result) {
                    case "1":
                        this.showBugPetPage()
                        break;
                    case "2":
                        this.showMyPetPage()
                        break;
                    case "3":
                        this.showWelcomePage()
                        break;
                    default:
                        arguments.callee.call(this)
                        break;
                }
            },
            //展示购买宠物界面
            showBugPetPage() {
                var has = this.checkHasPet()
                if (has) {
                    alert("你已经拥有了一个宠物，请好好对待" + (has.gender === "1" ? "他" : "她"))
                    this.showMainPage()
                    return
                }
                do {
                    var nickname = prompt("请输入一个昵称")
                    var isOk = !nickname || !nickname.trim()
                    if (isOk) alert("昵称不得为空")
                    var isUnallow = this.checkNameSensitive(nickname)
                    if (isUnallow) alert("用户名包含敏感词，请重新输入")
                } while (isOk || isUnallow)
                do {
                    var gender = prompt("请选择你宠物的性别：1.GG，2.MM")
                    var isOk = ["1", "2"].includes(gender)
                    if (!isOk) alert("选择有误，请重新选择")
                } while (!isOk)
                this.doBuyPet(nickname, gender, this.userLogin)
                this.showMainPage()
            },
            //展示我的宠物界面
            showMyPetPage() {
                var has = this.checkHasPet()
                if (!has) {
                    alert("你还没有宠物，快去领养一只吧")
                    this.showMainPage()
                    return
                }
                has.introduceSelf()
                var result = prompt("请选择你的操作：1.喂食，2.玩耍，3.返回上一步")
                switch (result) {
                    case "1":
                        this.doFeed(has)
                        break;
                    case "2":
                        this.doPlay(has)
                        break;
                    case "3":
                        return this.showMainPage()
                    default:
                        console.log("操作有误")
                        break;
                }
                arguments.callee.call(this)
            },
            //宠物操作界面
            doMyPet() {

            },
            //喂食操作
            doFeed(has) {
                has.eat()
            },
            //玩耍操作
            doPlay(has) {
                has.play()
            },
            //游戏开始函数
            start() {
                //模拟测试用户
                for (i = 0; i < 5; i++) {
                    this.doRegister("user" + i, "123123")
                }
                this.showWelcomePage()
            }
        }
        game.start()

    </script>
</body>

</html>